package com.magicalcoder.youyamvc.app.logadminoperate.service.impl;

import com.alibaba.fastjson.JSON;
import com.magicalcoder.youyamvc.app.adminuser.dto.AdminUserDto;
import com.magicalcoder.youyamvc.app.adminuser.util.AdminUserContextUtil;
import com.magicalcoder.youyamvc.app.logadminoperate.dao.LogAdminOperateProxyDao;
import com.magicalcoder.youyamvc.app.logadminoperate.service.LogAdminOperateService;
import com.magicalcoder.youyamvc.app.logadminoperate.service.LogAdminOperateProxyService;
import com.magicalcoder.youyamvc.app.model.LogAdminOperate;

import com.magicalcoder.youyamvc.core.common.utils.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
    代理类 保证了service dao的自动生成 有改动业务 直接写在此类处
*/
/**
* 代码为自动生成 Created by www.magicalcoder.com
*  欢迎加入官方QQ群:323237052
*/

@Component("logAdminOperateProxyService")
public class LogAdminOperateProxyServiceImpl implements LogAdminOperateProxyService{

    @Resource(name="logAdminOperateProxyDao")
    private LogAdminOperateProxyDao logAdminOperateProxyDao;

    @Resource(name="logAdminOperateService")
    private LogAdminOperateService logAdminOperateService;


    @Override
    public <T> void insertIntoLog(AdminUserDto adminUserDto, String tableName, String operateType, String primaryIdValue, String url, T entity) {
        Date now = new Date();
        LogAdminOperate log = new LogAdminOperate();
        log.setAdminUserId(adminUserDto.getId());
        log.setUserName(adminUserDto.getRealName());
        log.setCreateTime(now);
        log.setFormBody(JSON.toJSONString(entity,true));
        log.setUrl(url);
        log.setTableName(tableName);
        log.setOperateType(operateType);
        log.setPrimaryIdValue(primaryIdValue);
        logAdminOperateService.insertLogAdminOperate(log);
    }

    @Override
    public <T> void insertIntoLog(HttpServletRequest request,String operateType, String primaryIdValue, T entity) {
        AdminUserDto adminUserDto = AdminUserContextUtil.getLoginUserDto(request);
        if (adminUserDto != null) {
            String url = request.getServletPath();
            if (StringUtils.isNotBlank(request.getQueryString())) {
                url += "?" + request.getQueryString();
            }

            Date now = new Date();
            LogAdminOperate log = new LogAdminOperate();
            log.setAdminUserId(adminUserDto.getId());
            log.setUserName(adminUserDto.getRealName());
            log.setCreateTime(now);
            log.setFormBody(JSON.toJSONString(entity, true));
            log.setUrl(url);
            //还原历史记录 只支持带主键的表
            log.setOperateType(operateType);
            if (url.startsWith("/admin/")) {
                String[] arr = url.split("/");
                if (arr.length > 2) {
                    log.setTableName(arr[2]);
                }
            }
            log.setPrimaryIdValue(primaryIdValue);
            logAdminOperateService.insertLogAdminOperate(log);
        }
    }
    @Override
    public     List<LogAdminOperate> getSimpleList(String tableName,String primaryIdValue){
        Map<String,Object> query = new HashMap();
        query.put("tableName",tableName);
        query.put("primaryIdValue",primaryIdValue);
        return logAdminOperateProxyDao.getSimpleList(query);
    }
}
